knitr::opts_chunk$set(echo = TRUE)

library("smacof")
library("rgl")

flag = FALSE

MDS of Wine Scaling Data

Read in the data from file “delta.csv” which constains an nstim x nstim matrices of dissimilarities between 10 stimuli. Your first task is to carry out these four steps:

  1. df - the data.frame of the matrix
  2. set the row names equal to the column names
  3. nstim - the number of stimuli
  4. delta - transform the input data into a distance matrix
# insert code here

MDS

Compute a symetrical multidimensional scaling solution in 2, 3, 4, 5, 6, 7, 8, 9 dimensions using the assumption of “ordinal” data. Call the scaling results res2, res3, … res8

# insert code here

Plot of Stress for Dimensions

Make a vector of dimension number from 2 to 9 and another vector of corresponding stress values. The stress stored in res$stress

  1. x - integers 2 through 9
  2. y - stress for corresponding dimensions

Plot the stress as a function of dimensions. Label the x and y axes appropriately. How many dimensions would you choose?

# insert code here

Shepard Plots

Make Shepard plots of each of the solutions (2D…9D). Put these eight graphs in a two row by four column array of graphs using the graphic parameter: par(mfrow = c(2, 8)).

# insert code here

2D Plot of 4D solution

Let’s work with the 4D solution. First plot the first two dimensions of the 4D solution. Make the plot square by setting the graphics parameter pty to “s” par(pty = "s"). After doing the plot, reset that parameter to “m”

# insert code here

The first 5 stimuli are white wines, the second 5 are red wines. Make the plot points orange for white wines and red for red wines (yellow is hard to see against a white background).

  1. stim_colors - vector of 8 color names
  2. replot the above 2D graph using the colors
# insert code here

3D Plot

Draw 3D plot of the first 3 dimensions using plot3d() from the rgl package. Add the labels using text3d().
# insert code here

Property Analysis

Now read in the ratings of the wines on 13 quality dimensions and store them in a data frame named qual. These ratings are in the file qualities.csv. The first column is the Wine name (A, B, etc.). Display the contents of the data frame.

# insert code here

Use the smacof function biplotmds() to compare the properties with the dimensions. You will need to use smacof version 1.8 because biplotmds() is not in version 1.7. You can install my version (download from the course website) or install the CRAN version. Store the result of biplotmds() in object biprops. Use the plot() command to make a 2D plot of dimensions 1 and 2.

# insert code here

3D Property Plot

Now make a 3D plot with the quality vectors added

# insert code here

Cluster Analysis

Hierarchical Clustering

First do a hierarchical clustering to get an idea of how many clusters there are in the wine data Use hclust() that is included in the basic R installation. Remember that wines A, B, C, D, and E are white wines and that F, G, H, I, and J are red wines. Use the complete method for defining the distance between clusters. Save the results in object fit.complete. Plot the hierarchical cluster solution. Feel free to try other methods as well. Use help(hclust) to get more information about the options.

# insert code here

K-Means Clustering

Carry out a k-means clustering on the wine dissimilarities that are in object delta using kmeans() clustering. Form 2, 3 , 4 and 5 clusters, storing the results in km2, km3, km4, and km5 respectively

# insert code here

Plot the results of the k-means analyses.

# insert code here